//
//  LKRadarView.m
//  YLHelper
//
//  Created by she1323 on 2018/5/22.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKRadarView.h"


@interface LKRadarView()

@property (nonatomic, strong) RadarChartView *chartView;

@end


@implementation LKRadarView


+(instancetype)cretaeRadarViwWithFrame:(CGRect)frame andtitilesArray:(NSArray *)titlesArray AndValuesData:(NSArray *)valuesData AndColorViews:(NSArray *)colorsArray AndTypeValues:(NSArray *)typeValues{
    LKRadarView *view = [[LKRadarView alloc] initWithFrame:frame];
    view.titlesArray = titlesArray;
    RadarChartView *chartView = [[RadarChartView alloc] initWithFrame:view.bounds];
    [view addSubview:chartView];
    chartView.chartDescription.enabled = NO;
    chartView.webLineWidth = 1.0;
    chartView.innerWebLineWidth = 1.0;
    chartView.webColor = UIColor.redColor;
    chartView.innerWebColor = UIColor.purpleColor;
    chartView.webAlpha = 1.0;
    
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:9.f];
    xAxis.xOffset = 0.0;
    xAxis.yOffset = 0.0;
    xAxis.valueFormatter = view;
    xAxis.labelTextColor = UIColor.cyanColor;
    
    
    ChartYAxis *yAxis = chartView.yAxis;
    yAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:9.f];
    yAxis.labelCount = 5;
    yAxis.axisMinimum = 0.0;
    yAxis.axisMaximum = 50.0;
    yAxis.drawLabelsEnabled = NO;
    yAxis.drawGridLinesEnabled = NO;
    yAxis.drawAxisLineEnabled = NO;
    
    ChartLegend *l = chartView.legend;
    l.horizontalAlignment = ChartLegendHorizontalAlignmentCenter;
    l.verticalAlignment = ChartLegendVerticalAlignmentTop;
    l.orientation = ChartLegendOrientationHorizontal;
    l.drawInside = NO;
    l.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
    l.xEntrySpace = 7.0;
    l.yEntrySpace = 5.0;
    l.textColor = UIColor.blueColor;
    
    double mult = 80;
    double min = 20;
    NSInteger cnt = titlesArray.count;
    NSMutableArray *dataSetArray = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < valuesData.count; i++) {
        NSArray *valueArra = valuesData[i];
        NSMutableArray *entries = [[NSMutableArray alloc] init];
        for (NSInteger j = 0; j < valueArra.count; j++) {
            double value = [valueArra[j] doubleValue];
            [entries addObject:[[RadarChartDataEntry alloc] initWithValue:value]];
        }
        RadarChartDataSet *set = [[RadarChartDataSet alloc] initWithValues:entries label:typeValues[i]];
        [set setColor:colorsArray[i]];
        set.fillColor = colorsArray[i];
        set.drawFilledEnabled = YES;
        set.fillAlpha = 0.7;
        set.lineWidth = 2.0;
        set.drawHighlightCircleEnabled = YES;
        [set setDrawHighlightIndicators:NO];
        [dataSetArray addObject:set];
    }
    RadarChartData *data = [[RadarChartData alloc] initWithDataSets:dataSetArray];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:8.f]];
    [data setDrawValues:NO];
    data.valueTextColor = UIColor.whiteColor;
    chartView.data = data;
    return view;
}





#pragma mark - IAxisValueFormatter

- (NSString *)stringForValue:(double)value
                        axis:(ChartAxisBase *)axis
{
    return self.titlesArray[(int) value % self.titlesArray.count];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
